package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    //点赞功能
    @Override
    public Integer save(Comment comment1) {
            //1、查询动态
        Movement movement = mongoTemplate.findById(comment1.getPublishId(), Movement.class);
        //2、向comment对象设置被评论人属性
        if (movement!=null){
            comment1.setPublishUserId(movement.getUserId());
        }
        //3、保存到数据库
        mongoTemplate.save(comment1);
        //4、更新动态表中的对应字段
        Query query = Query.query(Criteria.where("id").is(comment1.getPublishId()));
        Update update = new Update();
        if(comment1.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount",1);
        }else if (comment1.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount",1);
        }else {
            update.inc("loveCount",1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//获取更新后的最新数据
        //5、获取最新的评论数量，并返回
        Movement andModify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        Integer integer = andModify.statisCount(comment1.getCommentType());
        return integer;
    }

    //分页查询评论列表
    @Override
    public List<Comment> findComments(String movementId, CommentType comment, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(comment.getType()))
                .skip((page-1)*pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Comment> list = mongoTemplate.find(query, Comment.class);
        return list;
    }
    //查询是否点赞
    @Override
    public Boolean hasComment(String movementId, Long userId, CommentType like) {
        Criteria criteria = Criteria.where("userId").is(userId).and("publishId").is(new ObjectId(movementId)).and("commentType").is(like.getType());
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query,Comment.class);//判断数据是否存在
    }

    //取消点赞
    @Override
    public Integer delete(Comment comment) {
        //1、删除Comment表数据
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);
        //2、修改动态表中的总数量
        Query movementQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if(comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount",-1);
        }else if (comment.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;//获取更新后的最新数据
        Movement modify = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);

        return modify.statisCount(comment.getCommentType());
    }


    //根据type查询是点赞还是评论还是喜欢
    @Override
    public List<Comment> findById(Integer page, Integer pagesize, Long userId, int type) {
       Criteria criteria = Criteria.where("publishUserId").is(userId).and("commentType").is(type);
        Query query = Query.query(criteria).skip((page-1)*pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Comment> list = mongoTemplate.find(query,Comment.class);

        return list;
    }

    @Override
    public Comment findByIdComment(String id,Long userId) {
        Criteria criteria = Criteria.where("id").is(id).and("commentType").is(2);
        Query query = Query.query(criteria);
        Comment comment = mongoTemplate.findOne(query, Comment.class);
        List<Long> commentLike = comment.getCommentLike();
        if (CollUtil.isEmpty(commentLike)){
            commentLike.add(userId);
        }else {
            for (Long aLong : commentLike) {
                if (aLong != userId){
                    commentLike.add(userId);
                }
            }
        }

        Update update = new Update();
        update.set("commentLike",commentLike);
        update.inc("likeCount",1);
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(false) ;//不获取更新后的最新数据
        Comment andModify = mongoTemplate.findAndModify(query, update, options, Comment.class);
        andModify.setLikeCount(comment.getLikeCount()+1);
        return andModify;
    }

}
